<template>
  <!-- 整体页面 -->
  <div class="login-container">
    <!-- 白色框 -->
    <div class="login-box">
      <div class="avatar-box">
        <!-- 图片 -->
        <img src="../assets/logo.png" alt />
      </div>
      <!-- 信息框 -->
      <div class="el-form">
        <el-form
          :model="ruleForm"
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="用户名称" prop="username">
            <el-input v-model="ruleForm.username"></el-input>
          </el-form-item>
          <el-form-item label="用户密码" prop="password">
            <el-input v-model="ruleForm.password"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
            <el-button @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      ruleForm: {
        username: "admin",
        password: "123456"
      },
      rules: {
        username: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" }
        ],
        password: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
          { min: 3, max: 6, message: "长度在 3 到 6 个字符", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    // 提交
    submitForm(formName) {
      this.$refs[formName].validate(async valid => {
        if (valid) {
          // 请求接口
          let { data: res } = await this.$http.post("/login", this.ruleForm);
          console.log(res);
          if (res.meta.msg == "登录成功") {
            //存token
            sessionStorage.setItem("token", res.data.token);
            //存用户名
            sessionStorage.setItem("user", JSON.stringify(res.data));
          }
          this.$message({
            message: "恭喜你，登录成功",
            type: "success"
          });
          this.$router.push("/home");
        } else {
          return false;
        }
      });
    },
    // 重置
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },

  mounted() {}
};
</script>
<style lang="scss">
.login-container {
  background-color: #2b4b6b;
  height: 100%;
}
.login-box {
  width: 450px;
  height: 304px;
  border-radius: 3px;
  top: 50%;
  background-color: #fff;
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
  box-shadow: 0 0 10px #ddd;
}
.avatar-box {
  width: 130px;
  height: 130px;
  padding: 10px;
  border: 1px solid #efefef;
  border-radius: 50%;
  background-color: #fff;
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
  box-shadow: 0 0 10px #ddd;
  img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    background-color: #efefef;
  }
}
.el-form {
  position: absolute;
  bottom: 10px;
  left: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}
</style>
